import cv2
import numpy as np

if __name__ == '__main__':
    # 1. 图片输入
    image_np = cv2.imread('work3.jpg')
    image_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
    _, image_thresh = cv2.threshold(image_gray, 192, 255, cv2.THRESH_OTSU, + cv2.THRESH_BINARY)

    conunters, hierarchy = cv2.findContours(image_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    for hier in hierarchy:
        print(hier)

    for cnt in conunters:
        print(cnt.shape)
        if 1000 < cnt.shape[0] <2000 :
            # x, y, z = map(int, np.random.randint(0, 256, 3))
            image_np = cv2.drawContours(image_np, cnt, contourIdx= -1, color=(0, 255, 0), thickness=2)
    #
    cv2.imshow('', image_np)
    cv2.imwrite('zuoye3.jpg', image_np)
    cv2.waitKey(0)